﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mighty.Common;


namespace Mighty.Brain
{


    public class NoBrain : BasicBrain
    {
        int[] card_shape = new int[4];
        bool joker0 = false;
        bool mighty = false;
        bool jokercall = false;
        int shape_num = 0;
        int min;
        int max;
        int max_shape;
        int min_shape;
       


        //카드를 정리하고 이전의 베팅 숫자를 확인한 후 베팅한 사람이 있으면 그보다 1 높여 베팅한다. 그러나 16까지만 배팅한다.
        public override Mighty.Common.Betting BettingTicEvent()
        {
            Betting betting = new Betting();
            
            
            for (int a = 0; a < 4; a++)
                card_shape[a] = 0;

            for (int k = 0; k < 10; k++)
            {
                HandedCard hc = gameInfo.myCards[k];
                if (gameInfo.myCards[k].number > 9)
                    shape_num++;

                if (gameInfo.myCards[k].shape == Shape.Clover)
                    card_shape[0]++;

                if (gameInfo.myCards[k].shape == Shape.Heart)
                    card_shape[1]++;

                if (gameInfo.myCards[k].shape == Shape.Diamond)
                    card_shape[2]++;

                if (gameInfo.myCards[k].shape == Shape.Spade)
                    card_shape[3]++;

                if (gameInfo.myCards[k].shape == Shape.Joker)
                    joker0 = true;

                if (gameInfo.myCards[k].shape == Shape.Clover && gameInfo.myCards[k].number == 3)
                    jokercall = true;

                if (gameInfo.myCards[k].shape == gameInfo.mighty.shape && gameInfo.myCards[k].number == 14)
                    mighty = true;
            }




            //betting.isPass = true; //항상 pass를 선언하는 경우


            // 무늬가 7장 이상이거나 같은 무늬가 5장 이상이면 배팅을 한다. 나머지 경우는 pass

            Betting previousBetting;

            if (shape_num >= 7 || card_shape[0] >= 6 || card_shape[1] >= 6 || card_shape[2] >= 6 || card_shape[3] >= 6 && betting.limit < 16)
            {
                betting.isPass = false;
                int s = No_Max();
                Giruda maximum = Giruda.NoGiruda;

                if (s == 0)
                    maximum = Giruda.Clover;

                else if (s == 1)
                    maximum = Giruda.Heart;

                else if (s == 2)
                    maximum = Giruda.Diamond;

                else if (s == 3)
                    maximum = Giruda.Spade;



                if (gameInfo.betting.limit > 0)
                {
                    previousBetting = (Betting)gameInfo.bettingHistory[gameInfo.bettingHistory.Count - 1];
                    betting.giruda = maximum;
                    betting.limit = previousBetting.limit + 1;

                }
                else
                {
                    betting.limit = 13;
                    betting.giruda = maximum;
                }
                

            }
            else
                betting.isPass = true;
            
            return betting;
        }


        // 주공이 된후 카드를 3장 들고 가장 적은 카드를 버린다. 
        public override HandedCard[] ChoiceCardEvent()
        {
       
            
            for (int k = 10; k < 12; k++)
            {
                HandedCard hc = gameInfo.myCards[k];
                if (gameInfo.myCards[k].number > 9)
                    shape_num++;

                if (gameInfo.myCards[k].shape == Shape.Clover)
                    card_shape[0]++;

                if (gameInfo.myCards[k].shape == Shape.Heart)
                    card_shape[1]++;

                if (gameInfo.myCards[k].shape == Shape.Diamond)
                    card_shape[2]++;

                if (gameInfo.myCards[k].shape == Shape.Spade)
                    card_shape[3]++;

                if (gameInfo.myCards[k].shape == Shape.Joker)
                    joker0 = true;

                if (gameInfo.myCards[k].shape == Shape.Clover && gameInfo.myCards[k].number == 3)
                    jokercall = true;

                if ((int)hc.shape == (int)gameInfo.betting.giruda && hc.number == 14)
                    mighty = true;

            }
            for (int i = 0; i < 3; i++)
            {
                int l;
                l = No_Min();
                Shape minimum = Shape.Clover;

                for (int j = 0; j < 13; j++)
                {
                    if (l == 0)
                        minimum = Shape.Clover; 
                    
                    else if (l == 1)
                        minimum = Shape.Heart; 
                    
                    else if (l == 2)
                        minimum = Shape.Diamond; 
                    
                    else if (l == 3)
                        minimum = Shape.Spade; 
                    



                    if (minimum == gameInfo.myCards[j].shape && gameInfo.myCards[i].number!=14)
                    {
                        gameInfo.myCards[j].cmdRemove = true;
                        break;
                    }
                }
            }
            return null;
        }


       
        public int No_Max()
        {
            max = 0;
            max_shape = 0;
            for (int i = 0; i < 4; i++)
            {
                if (card_shape[i] > max)
                {
                    max = card_shape[i];
                    max_shape = i;
                }
            }
            return max_shape;

        }

        public int No_Min()
        {
            min = 100;
            min_shape = 0;
            for (int i = 0; i < 4; i++)
            {
                if (card_shape[i] < min)
                {
                    min = card_shape[i];
                    min_shape = i;
                }
            }
            return min_shape;

        }
        public override FriendPlayer ChoiceFriendEvent()
        { 
            FriendPlayer friendPlayer = new FriendPlayer();
            HandedCard hc = gameInfo.myCards[10];
           /* for ( int i = 0 ; i < gameInfo.myCards ; i++ ) {
                
            }
            
            bool haveMighty = false;
            foreach ( HandedCard hc in gameInfo.myCards) {

                if ( hc.shape == Shape.Joker );
                if ( hc.shape == gameInfo.mighty.shape && hc.number == 14 )
                if ( (int)hc.shape == (int)gameInfo.betting.giruda && hc.number == 14 )  


            }*/

            Card jokerr = new Card();
            jokerr.shape = Shape.Joker;

            if (mighty == false)
            {
                friendPlayer.howToSelect = FriendPlayer.HowToSelect.card;
                friendPlayer.cardCondition = gameInfo.mighty;
                return friendPlayer;
            }
            else if (joker0 == false)
            {
                friendPlayer.howToSelect = FriendPlayer.HowToSelect.card;
                friendPlayer.cardCondition = jokerr;
                return friendPlayer;
            }
            else if ( (int)hc.shape == (int)gameInfo.betting.giruda && hc.number == 14 )
            {
                friendPlayer.howToSelect = FriendPlayer.HowToSelect.card;
                friendPlayer.cardCondition = gameInfo.mighty;
                return friendPlayer;
            }
            else
            {
                gameInfo.friend.howToSelect = FriendPlayer.HowToSelect.firstwin;
                return gameInfo.friend;
            }


 
            
        }

        // 각 round에서 내야 할 카드를 결정한다. 경우는 크게 첫번째로 내는 경우와 그렇지 않은 경우가 있다. 

        public override HandedCard RoundTicEvent()
        {
            Round r = gameInfo.roundHistory[gameInfo.roundIdx];
            bool first_play = No_Search();

            if (first_play)
            {
                int maxNumber = 1;
                int index = -1;
                for (int i = 0; i < 10; i++)
                {
                    HandedCard hc = gameInfo.myCards[i];

                    if (hc.isPossible)
                    {
                        if (hc.number > maxNumber)
                        {
                            maxNumber = hc.number;
                            index = i;
                        }
                    }
                }
                if (index != -1) return gameInfo.myCards[index];

             }

            bool firstshape = No_Firstshape();

            if (firstshape)
            {
                bool havegiruda = No_havegiruda();
                //카드 스택 안에 기루다가 있는 경우 와 그렇지 않은 경우 
                if (havegiruda)
                {
                    int minNumber = 14;
                    int index0 = -1;
                    for (int i = 0; i < 10; i++)
                    {
                        HandedCard hc = gameInfo.myCards[i];

                        if (hc.isPossible)
                        {
                            if (hc.number < minNumber)
                            {
                                minNumber = hc.number;
                                index0 = i;
                            }
                        }
                    }
                    return gameInfo.myCards[index0];
                }

                int max = 0;
                bool have_max = false;
                int index1 = -1;
                //cardstack에 있는 수 중 가장 큰 수 찾기.
                for(int i=0; i<5; i++)
                {
                    if (r.cardStack[i] == null) break;
                    if (r.cardStack[i].number > max)
                        max = r.cardStack[i].number;

                   
                }
                //cardstack에 있는 수 중 가장 큰 수보다 더 큰 수가 있는지 확인하기.
                for(int j=0; j<10 ; j++)
    
                    {
                        HandedCard hc = gameInfo.myCards[j];

                        if (hc.isPossible)
                        {
                            if (hc.number > max)
                            {
                                have_max = true;
                                index1 = j;
                            }

                            
                        }
                    }

                if (have_max)
                    return gameInfo.myCards[index1];

                int minNumber2 = 14;
                int index2 = -1;
                for (int i = 0; i < 10; i++)
                {
                    HandedCard hc = gameInfo.myCards[i];

                    if (hc.isPossible)
                    {
                        if (hc.number < minNumber2)
                        {
                            minNumber2 = hc.number;
                            index2 = i;
                        }
                    }
                }
                return gameInfo.myCards[index2];


            }

                int maxNum = 1;
                int index_ = -1;
                for (int i = 0; i < 10; i++)
                {
                    HandedCard hc = gameInfo.myCards[i];

                    if (hc.isPossible)
                    {
                        if (hc.number > maxNum)
                        {
                            maxNum = hc.number;
                            index_ = i;
                        }
                    }
                }
               return gameInfo.myCards[index_];

            
            

          }
          

            
            /*
            bool ace = false;
            for (int i = 0; i < 5; i++)
            {
                RoundCard rc = r.cardStack[i];
                if (rc == null) break;

            }

            foreach (RoundCard rc in r.cardStack)
            {
                if (rc == null) continue;
                if (rc.number == 14) ace = true;

            }

            int minNumber = 14;
            int index = -1;
            for (int i = 0; i < 10; i++)
            {
                HandedCard hc = gameInfo.myCards[i];

                if (hc.isPossible)
                {
                    if (hc.number < minNumber)
                    {
                        minNumber = hc.number;
                        index = i;
                    }
                }
            }
            if (ace && index != -1) return gameInfo.myCards[index];

            return null;*/

        

        //선의 여부를 확인하는 함수(선이면 true)
        public bool No_Search()
        {
            bool search = false;
            Round r = gameInfo.roundHistory[gameInfo.roundIdx];
            if (r.cardStack[0] == null)
                search = true;
            
            return search;

        }
        // 바닥의 첫번째 카드가 나에게 있는지 확인하는 함수 (있으면 true)
        public bool No_Firstshape()
        {
            bool firstshape = false;
            Round r = gameInfo.roundHistory[gameInfo.roundIdx];
            for(int i = 0 ; i<10;i++)
            {
                if (r.cardStack[0].shape == gameInfo.myCards[i].shape)
                {
                    firstshape = true;
                    break;
                }
            }
            return firstshape;
        }


        public bool No_havegiruda()
        {
            bool havegiruda = false;
            Round r = gameInfo.roundHistory[gameInfo.roundIdx];

            for (int i = 0; i < 5; i++)
            {
                if (r.cardStack[i] == null) break;
                if (r.cardStack[i].shape == (Shape)gameInfo.betting.giruda)
                    havegiruda = true;
            }
            return havegiruda;

         }
    } 
}
